<?php

namespace App\Admin\Controllers\Used;

use App\Admin\Actions\Grid\UsedGoodsPassAction;
use App\Admin\Repositories\Used\WxUsedGood;
use App\Models\Used\WxUsedClassify;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxUsedGoodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxUsedGood(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->model()->orderBy('id', 'desc');
            $grid->quickSearch(['id', 'title', 'detail', 'classify_id','user_id','user.user_name'])->placeholder('搜索ID，标题，简介，用户ID，用户名字，二手分类ID...')->width(50);
            $grid->column('owers', '头像')->display(function ($v) {
                return '<img src="' . ($this->ower['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('ower', '用户名称')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('title')->limit(20);
            $grid->column('detail')->limit(40);
            $grid->column('classify_id')->display(function ($v){
                if($v > 0){
                    return $v;
                }
                return '';
            })->sortable();
            $grid->column('price')->sortable();
            $grid->column('original_price')->sortable();
            $grid->column('is_self_pickup')->using([0 => '', 1 => '是'])->sortable();
            $grid->column('is_express_delivery')->using([0 => '', 1 => '是'])->sortable();
            $grid->column('express_cost')->sortable();
//            $grid->column('image_urls');
//            $grid->column('longitude');
//            $grid->column('latitude');
//            $grid->column('address_name');
//            $grid->column('address_detailed');
//            $grid->column('ip');
//            $grid->column('country');
//            $grid->column('province');
//            $grid->column('city');
//            $grid->column('district');
            $grid->column('status')->using(FieldUtils::usedGoodState())->label([
                0 => 'red',
                1 => 'green',
                2 => 'default',
                3 => '#4e9876',
                4 => 'default',
                5 => '#21b978',
                6 => 'default',
                7 => 'default'
            ])->sortable();;
            $grid->column('created_at')->sortable();
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->batchActions(function ($batch) {
                $batch->add(new UsedGoodsPassAction());
            });

            $grid->disableViewButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('user_id');
                $filter->equal('classify_id')->select(WxUsedClassify::where('status', 1)->orderByDesc('order')->orderByDesc('id')->pluck('name', 'id'));
                $filter->between('created_at')->date();
            });

            $grid->disableCreateButton();
//            $grid->disableActions();
            // 禁用编辑按钮
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxUsedGood(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('title');
            $show->field('detail');
            $show->field('classify_id');
            $show->field('price');
            $show->field('original_price');
            $show->field('is_self_pickup');
            $show->field('is_express_delivery');
            $show->field('express_cost');
            $show->field('image_urls');
            $show->field('longitude');
            $show->field('latitude');
            $show->field('address_name');
            $show->field('address_detailed');
            $show->field('ip');
            $show->field('country');
            $show->field('province');
            $show->field('city');
            $show->field('district');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxUsedGood(), function (Form $form) {
            $form->display('id');
//            $form->text('user_id');
            $form->text('title');
            $form->textarea('detail');
            $form->select('classify_id')->options(WxUsedClassify::where('status', 1)->orderByDesc('order')->orderByDesc('id')->pluck('name', 'id'));
            $form->text('price');
            $form->text('original_price');
//            $form->text('is_self_pickup');
//            $form->text('is_express_delivery');
            $form->text('express_cost');
//            $form->text('image_urls');
//            $form->text('longitude');
//            $form->text('latitude');
//            $form->text('address_name');
//            $form->text('address_detailed');
//            $form->text('ip');
//            $form->text('country');
//            $form->text('province');
//            $form->text('city');
//            $form->text('district');
//            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });
        });
    }
}
